package com.chuangsi.project.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangsi.project.admin.domain.TLog;
import com.chuangsi.project.admin.dto.admin.log.LogPageQueryRes;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;

/**
 * <p>
 * 操作日志 Mapper 接口
 * </p>
 *
 * @author ocean
 * @since 2020-02-06
 */
public interface TLogMapper extends BaseMapper<TLog> {

    @Update("<script>" +
            "INSERT INTO ${tableName} (`id`, `user_name`, `module`, `methods`, `content`, `actionurl`, `ip`, `commite`, `msg`) VALUES (#{id}, #{userName}, #{module}, #{methods}, #{content}, #{actionurl}, #{ip}, #{commite}, #{msg})" +
            "</script>")
    void insert(String id,String tableName, String actionurl, String commite, String content, String ip, String methods, String module, String userName, String msg);

    @Select("<script>" +
            "SELECT l.* " +
            "FROM ${tableName} l " +
            "<where>" +
            "<if test=\"userName!=null &amp;&amp; userName!=''\">" +
            "AND l.user_name LIKE CONCAT('%',#{userName},'%') " +
            "</if>" +
            "<if test='startTime!=null'>" +
            "AND l.create_time &gt;= #{startTime} " +
            "</if>" +
            "<if test='endTime!=null'>" +
            "AND l.create_time &lt;= #{endTime} " +
            "</if>" +
            "</where>" +
            "ORDER BY l.create_time DESC " +
            "</script>")
    IPage<LogPageQueryRes> logPageQuery(Page<LogPageQueryRes> logPageQueryResPage, String userName, LocalDateTime startTime, LocalDateTime endTime, String tableName);

    @Update("<script>" +
            "CREATE TABLE IF NOT EXISTS ${tableName} ( " +
            " `id` CHAR(16) NOT NULL COMMENT 'ID', " +
            " `user_name` VARCHAR(20) NULL DEFAULT NULL COMMENT '操作人用户名 关联（sys_user nick_name）', " +
            " `module` VARCHAR(50) NOT NULL COMMENT '操作模块', " +
            " `methods` VARCHAR(50) NOT NULL COMMENT '操作方法', " +
            " `content` MEDIUMTEXT NOT NULL COMMENT '操作内容', " +
            " `actionurl` VARCHAR(255) NOT NULL COMMENT '请求路径', " +
            " `ip` VARCHAR(255) NOT NULL COMMENT '请求IP', " +
            " `commite` CHAR(1) NOT NULL COMMENT '操作状态描述（1:操作成功、2:操作失败）', " +
            " `msg` VARCHAR(50) NOT NULL COMMENT '返回信息', " +
            " `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', " +
            " `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', " +
            " PRIMARY KEY (`id`) " +
            ") " +
            "COMMENT='操作日志' " +
            "COLLATE='utf8mb4_general_ci' " +
            "ENGINE=InnoDB; " +
            "</script>")
    void ifExisted(@Param("tableName") String tableName);
}
